<?php
class Section extends Admin_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->Model(array("Msection"));
		$this->breadcrumbs->push('Section', 'admincp/section/view');
	}
	
	public function view(){
        $data['icon_page']=$this->config->item('icon_view');
        $data['title']='Section Manager';
        $data['sub_title']='Section Manager';
        $data['columns'] = array(
        	'id' => trans('id'),
        	'title' => trans('title'),
        	'created_at' => trans('created_at')
        );
        $data['sections']=$this->Msection->listSection(array_keys($data['columns']));
		$this->parser->parse("section/view.tpl", NULL, $data);
	}

	public function add(){
		$data['icon_page']=$this->config->item('icon_create');
        $data['title']='Create New Section';
        $data['sub_title']='Create New Section';
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$title =  $this->input->post('sec_name');
			$data_insert = array(
				'title' => $this->input->post('sec_name'),
				'url' => url_title(convert_accented_characters($title), '-', TRUE),
				'created_at' => time(), 
			);
			$this->Msection->insert($data_insert);
			$this->session->set_flashdata(array("success"=>trans('message_create_successfully')));
			redirect(admincp_url()."section/view");		
		}
		$this->breadcrumbs->push('Create Section', 'admincp/section/add');
		$this->parser->parse("section/add.tpl", NULL, $data);
	}

	public function edit(){
		$data['icon_page']=$this->config->item('icon_edit');
        $data['title_page']='Edit Section';
        $data['sub_title_page']='Edit Section';
		$cache_id = $id = $this->uri->segment(4);
		$data['info'] = $this->Msection->get_data($id);
		if($this->input->server('REQUEST_METHOD') == "POST"){
			$title =  $this->input->post('sec_name');
			$data_update = array(
				'title' => $this->input->post('sec_name'),
				'url' => url_title(convert_accented_characters($title), '-', TRUE),
				'updated_at' => time(), 
			);
			$this->Msection->update($data_update,$id);
			$this->session->set_flashdata(array("success"=>trans('message_edit_successfully')));
			redirect(admincp_url()."section/view");
		}
		$this->breadcrumbs->push('Edit Section', 'admincp/section/edit/'.$id);
		$this->parser->parse("section/edit.tpl", $cache_id, $data);
	}

	public function delete(){
		$id = $this->uri->segment(4);
		$this->Msection->delete($id);
		$this->session->set_flashdata(array("success"=>trans('message_delete_successfully')));
		redirect(admincp_url()."section/view");
	}
	
	public function check_name($name,$sec_id){
		if($this->Msection->check_name($name, $sec_id) == FALSE){
			return FALSE;
		}else{
			return TRUE;
		}
	}
}